package eclipse.lib.graphics;

import java.net.URL;
import java.io.*;
import javax.media.opengl.GL;
import com.sun.opengl.util.texture.*;
import eclipse.util.*;
/**
 * The TextureLoader class takes the path of a texture and loads/streams it for use in the Application.
 */
public class GLTextureLoader
{
	/** 
	 * Loads and returns a PNG image from a stream
	 * @param iFile Stream for the image
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadPNG(InputStream iFile, boolean mipmap)
	{
		return loadIMG(iFile, mipmap, TextureIO.PNG);
	}
	/**
	 * Loads and returns a PNG image from a URL
	 * @param uFile URL path of the image
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadPNG(URL uFile, boolean mipmap)
	{
		return loadIMG(uFile, mipmap, TextureIO.PNG);
	}
	/** 
	 * Loads and returns a GIF image from an InputStream
	 * @param iFile Stream of the GIF
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadGIF(InputStream iFile, boolean mipmap)
	{
		return loadIMG(iFile, mipmap, TextureIO.GIF);
	}
	/**
	 * Loads and returns a GIF image from a URL
	 * @param uFile URL path of the image
	 * @param mipmap Use mipmaps 
	 */
	public static Texture loadGIF(URL uFile, boolean mipmap)
	{
		return loadIMG(uFile, mipmap, TextureIO.GIF);
	}
	/**
	 * Loads and returns a JPG image from a URL
	 * @param uFile URL path of the JPG
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadJPG(URL uFile, boolean mipmap)
	{
		return loadIMG(uFile, mipmap, TextureIO.JPG);
	}
	/**
	 * Loads and returns a JPG image from an InputStream
	 * @param iFile Stream of the JPG
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadJPG(InputStream iFile, boolean mipmap)
	{
		return loadIMG(iFile, mipmap, TextureIO.TGA);
	}
	/**
	 * Loads and returns a TGA image from a URL
	 * @param uFile URL path of the TGA image
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadTGA(URL uFile, boolean mipmap)
	{
		return loadIMG(uFile, mipmap, TextureIO.TGA);
	}
	/**
	 * Loads and returns a TGA image from an InputStream
	 * @param iFile Stream of the TGA image
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadTGA(InputStream iFile, boolean mipmap)
	{
		return loadIMG(iFile, mipmap, TextureIO.TGA);
	}	
	/**
	 * Loads and returns a TIFF image from an InputStream
	 * @param iFile Stream of the TIFF image
	 * @param mipmap Use mipmaps 
	 */
	public static Texture loadTIFF(InputStream iFile, boolean mipmap)
	{
		return loadIMG(iFile, mipmap, TextureIO.TIFF);
	}
	/**
	 * Loads and returns a TIFF image from a URL
	 * @param uFile URL path of the TIFF image
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadTIFF(URL uFile, boolean mipmap)
	{
		return loadIMG(uFile, mipmap, TextureIO.TIFF);
	}
	/**
	 * Loads and returns a DDS from a URL
	 * @param uFile URL path of the DDS file
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadDDS(URL uFile, boolean mipmap)
	{
		return loadIMG(uFile, mipmap, TextureIO.DDS);
	}
	/**
	 * Loads and returns a DDS from an INput Stream
	 * @param iFile InputStream of the DDS
	 * @param mipmap Use mipmap
	 */
	public static Texture loadDDS(InputStream iFile, boolean mipmap)
	{
		return loadIMG(iFile, mipmap, TextureIO.DDS);
	}
	/**
	 * Loads an Image from a URL
	 * @param uFile URL path for the image
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadIMG(URL uFile, boolean mipmap)
	{
		return loadIMG(uFile, mipmap, null);
	}
	/**
	 * Loads an image from an INputStream
	 * @param iFile Inputstream for the image
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadIMG(InputStream iFile, boolean mipmap)
	{
		return loadIMG(iFile, mipmap, null);
	}
	/**
	 * Loads an Image from an ERL path
	 * @param eFile ERL path for the Image
	 * @param mipmap Use mipmaps
	 */
	public static Texture loadIMG(ERL eFile, boolean mipmap)
	{
		return loadIMG(eFile, mipmap, null);
	}
	/**
	 * Loads an image from a URL with specified extension
	 * @param uFile URL path for the image
	 * @param mipmap Use mipmaps
	 * @param ext String extension to the image
	 */
	private static Texture loadIMG(URL uFile, boolean mipmap, String ext)
	{
		Texture text = null;
		try
		{
			
            text = TextureIO.newTexture(uFile, mipmap, ext);        
			text.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
			text.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return text;
	}
	/**
	 * Loads an image from an Input stream using specified extension
	 * @param iFile InputStream of the image
	 * @param mipmap use mipmaps
	 * @param ext String extension
	 */
	private static Texture loadIMG(InputStream iFile, boolean mipmap, String ext)
	{
		Texture text = null;
		try
		{
            text = TextureIO.newTexture(iFile, mipmap, null);        
			text.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
			text.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return text;
	}
	private static Texture loadIMG(ERL eFile, boolean mipmap, String ext)
	{
		return loadIMG(eFile.openStream(), mipmap, ext);
	}
}
